package api

import (
	"ContentSystem/internal/models"
	"ContentSystem/internal/utils"
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/redis/go-redis/v9"
	"net/http"
)

const SessionKey = "session_id"

type SessionAuth struct {
	rdb *redis.Client
}

func NewSessionAuth() *SessionAuth {
	s := &SessionAuth{}
	s.rdb = models.ConnRdb()
	return s
}

func (s *SessionAuth) Auth(ctx *gin.Context) {
	sessionID := ctx.GetHeader(SessionKey)
	if sessionID == "" {
		ctx.AbortWithStatusJSON(http.StatusUnauthorized, "session_id is empty")
		ctx.Abort()
	}
	authKey := utils.GetAuthKey(sessionID)
	//todo:sessionId 验证
	result, err := s.rdb.HGet(context.Background(), authKey, "userID").Result()
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusUnauthorized, "session_id not get redis")
		ctx.Abort()
	}
	fmt.Println(result)
	ctx.Next()
}
